package leetcode;

public class Minimum_Size_Subarray_Sum {
	public int minSubArrayLen(int s, int[] nums) {
		if (nums == null || nums.length == 0)
			return 0;
		int sum = nums[0], start = 0, end = 0;
		int min = Integer.MAX_VALUE;
		while (start != nums.length) {

			if (sum < s) {
				if (end < nums.length - 1) {
					end++;
					sum += nums[end];
				} else {
					return min == Integer.MAX_VALUE ? 0 : min;
				}
			} else {
				min = min > end - start + 1 ? end - start + 1 : min;
				if (start < end) {
					sum -= nums[start];
					start++;
				} else {
					start++;
					end++;
					if (start == nums.length)
						return min == Integer.MAX_VALUE ? 0 : min;
					else{
						sum = nums[start];
					}
				}
			}

		}
		return min == Integer.MAX_VALUE ? 0 : min;
	}

	public static void main(String[] argsStrings) {
		Minimum_Size_Subarray_Sum minimum_Size_Subarray_Sum = new Minimum_Size_Subarray_Sum();
		int nums[] = {22}; 
		System.out.println(minimum_Size_Subarray_Sum.minSubArrayLen(15, nums));
	}
}
